package com.hyjx.framework.component.message;

import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.common.util.StringHelper;
import com.hyjx.framework.orm.dao.IBaseDao;
import com.hyjx.framework.orm.dao.Pager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 消息服务实现类
 */
@Service
@Transactional
public class MessageServiceImpl implements MessageService {
    @Resource
    private IBaseDao dao;

    public String queryList(MessageModel model) {
        Pager pager = dao.getPager("message.queryList", "message.queryCount", model);
        return CommonUtil.getPageJsonStr(pager);
    }

    @Override
    public boolean add(MessageModel model) {
        dao.addObject("message.insert", model);
        MessageUserModel mum = new MessageUserModel();
        mum.setIsread("0");
        mum.setMsgid(model.getMsgid());
        mum.setMuid(StringHelper.getPrimaryKey());
        mum.setUserid(model.getTouser());
        mum.setReadtime("");
        dao.addObject("message.insertmsgusr", mum);
        return true;
    }

    @Override
    public MessageModel getInfoById(String s) {
        MessageModel model = null;
        model = (MessageModel) dao.queryObject("message.getInfoById", s);
        return model;
    }

    @Override
    public int edit(MessageModel model) {
        return dao.updateObject("message.update", model);
    }

    @Override
    public int delete(String ids) {
        String[] id_array = ids.split(",");
        int num = 0;
        for (String id : id_array) {
            dao.deleteObject("message.delete", id);
            num++;
        }
        return num;
    }

    @Override
    public void addmsgusr(MessageUserModel model) {
        dao.addObject("message.insertmsgusr", model);

    }

    @Override
    public String refreshMsgCount(String user_id) {
        return (String) dao.queryObject("message.refreshMsgCount", user_id);
    }

    @Override
    public void updateMsgIsread(MessageUserModel mu) {
        dao.updateObject("message.updateMsgIsread", mu);
    }

}
